<?php
/**
 * Created by PhpStorm.
 * User: 郝飞
 * Date: 2017/4/5
 * Time: 12:08
 * 描述:这个文件将来要删除操作,因为该目录下将来会有很多短信接口，而该文件是阿里大于短信接口，文件已改为 AlidayuController.class.php
 */
namespace Sms\Controller;
use Common\Controller\SmsbaseController;

class SmsController extends SmsbaseController{

    private $now_data;
    private $time_num;
    public function _initialize()
    {
        parent::_initialize();
        $this->now_data = Date('Y-m-d');
        $this->time_num = strtotime($this->now_data);
    }

    /**
     * @author haofei
     * 发送短信 生成安全标识码
     */
    public function encryption()
    {
        $identification = sms_password($this->time_num);
        $arr[] = $identification;
        $arr = json_encode($arr);
        echo $arr;
    }
    public function encryptions()
    {
        $identification = sms_password($this->time_num);
        $arr['code'] = $identification;
//        $arr = json_encode($arr);
        $this->output($arr);
    }

    /**
     *   发送短信
     */
    public function sms_send()
    {
        //HaoFei EDIT By 添加短息发送验证 start
        $identification = I('identification');
        if (empty($identification)) {
            $arr['status'] = '0';
            $arr['msg'] = '非法请求';
            $this->output($arr);
        } else {
            $status = sms_compare_password($this->time_num, $identification);
        }
        if (!$status) {
            $arr['status'] = '0';
            $arr['msg'] = '非法请求';
            $this->output($arr);
        }
        //HaoFei EDIT By 添加短息发送验证 end


        //step1:验证码 和 手机号
        $sms_recNum  = I('mobile');  		//手机号
        $rand_number = randNum(6);  // 验证码

        //step2:把 手机号和验证码 存入cookie中
        session('verificationMobile', $sms_recNum);	//手机号cookie
        session('verificationCode', $rand_number);	//验证码cookie

        //step3:短信安全配置 短信配置 及 发送
        import("AliMsgSend");
        $appKey = C('APPKEY');
        $secreKey = C('SECRETKEY');
        $re = new \AliMsgSend($appKey, $secreKey);

        $recNum = $sms_recNum; 															// 要发送的手机号码，注意这个是字符串类型的
        $smsParam = "{code:'".$rand_number."',product:'".C('SMS_PRODUCT')."'}"; 	// 模板变量
        $smsTemplateCode = C('SMS_TEMPLATECODE'); 									// 模板id
        $smsFreeSignName = C('SMS_SIGNNAME'); 											// 签名
        $res =$re->sendMsg($recNum, $smsParam, $smsTemplateCode, $smsFreeSignName);

        //step5:状态及输出，这里有问题
        if ($res) {
            $arr['status'] = '1';
            $arr['msg']='发送成功，验证码不要告诉其他人！';
        } else {
            $arr['status'] = '0';
            $arr['msg'] = '发送失败，可能你的账户存在问题！';  //这里只判断0和1两种状态，1 表示成功，0 可能是账户未开通/短信超过数量/加入黑名单 等等,根据籽官方手册查询
        }

        $this->output($arr);
    }


    /* 短信验证码校验 */
    public function verification(){

        //step1:cookie中取出验证码手机号
        $verificationCode = session('verificationCode');
        $verificationMobile = session('verificationMobile');

        //step2:用户传递验证码
        $verificationCodeCorr = I('code');
        $verificationMobileCorr = I('mobile');

        //step3:验证手机号和验证码状态及输出
        if($verificationCode == $verificationCodeCorr && $verificationMobile==$verificationMobileCorr){
            $arr['status'] = '1';
            $arr['msg'] = '验证码校验成功';
        } else {
            $arr['status'] = '0';
            $arr['msg'] = '验证码校验失败';
        }
        $this->output($arr);
    }

}